%% Reloads the data from the XLSX files in the Data folder
% Called from the Startmodel menu or directly.
% Filestructure of XLS files is rows = country, cols = year, tabs = mode
% Output is a structure exvars."xlsfilename"(r,t,m), saves in a mat file
%
function LoadXLSXData(varargin)
inputcell = {'Regions11Modes4','Regions26Modes7'};
results = SelectionMenu(inputcell); % Just a little selection window i made earlier

data={}; textdata={};
if length (varargin)==0; 0; end

mrset = inputcell{results};

fields      = {'Income'    'Population'    'UrbanPopulation'    'TravelPrice'...
    'Speed'    'TravelDemand'    'TMBCorrection'    'WageRate'    'LoadFactor'...
    'GasolinePrice'   'FuelPrice'   'NonEnergyCost'    'EnergyCost' ...
    'ERSaturation'   'EquipmentRate'   'Saturation', 'CO2Intensity','EnergyIntensity'}; % What fields are needed?

switch  mrset % Determine which set is needed
    case 'Regions26Modes7'
        dimensions      = {[27 130], [27 130], [27 130], [27 130 7], [27 130 7],...
        [27 35 7], [27 1 1], [27 130], [27 130 7], [27 130], [27 130], [27 130 7],[27 130 7],[27 1 1], [27 130 1],[27 1 1]};
        subfolder       = '26 Regions 7 Modes (Source)';
        range1          = 'B2:B28';
        range2          = 'B2:EA28';
        add             = '';
        exvars.ModeSet  = 'modes7';
        exvars.RegionSet= 'regions26';
    case 'Regions11Modes4'
        dimensions  = {[12 130], [12 130], [12 130], [12 130 4], [12 130 4],...
        [12 35 4], [12 1 1], [12 130], [12 130 4], [12 130], [12 130], [12 130 4], [12 130 4],[12 1 1], [12 130 1],[12 1 1],[12 130 4],[12 130 4]};
        subfolder = '11 Regions 4 Modes';
        range1    = 'B2:B13';
        range2    = 'B2:EA13';
        add       = '1'; % Needed to open both files in excel, doesn't accept same filenames
        exvars.ModeSet  = 'modes4';
        exvars.RegionSet= 'regions11';
end

for n = 1:length(fields)
    files = dir(['Data/' subfolder '/' add fields{n} '*']);
    file  = files(1).name;
    disp(['Processing ' file]);
    exvars.(fields{n}) = zeros(dimensions{n});
    for m = 1:size(exvars.(fields{n}),3)
        if size(exvars.(fields{n}),2) == 1        
            exvars.(fields{n})(:,:,m) = xlsread(['Data/' subfolder '/' file],m,range1);
        else
            exvars.(fields{n})(:,:,m) = xlsread(['Data/' subfolder '/' file],m,range2);
        end
    end
end

save(['Data/exvars 1971-2100 Original Dataset ' mrset '.mat'],'exvars')

exvars.Speed(:,:,:)=repmat(exvars.Speed(:,35,:),[1 130 1]);
save(['Data/exvars 1971-2100 Constant Speed ' mrset '.mat'],'exvars')

end